package com.example.config;

import com.example.interceptor.MyAnnoInterceptor;
import com.example.interceptor.MyInterceptor;
import com.example.interceptor.advisor.MyAnnotationAdvisor;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import java.util.Vector;

/**
 * 配置自定义切面
 */
@Configuration
public class MyInterceptorConfig {

    //注意该地址为项目具体包地址
    public static final String traceExecution = "execution(* com.example.controller..*.*(..))";
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor2() {
        MyInterceptor interceptor = new MyInterceptor();
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(traceExecution);

        // 配置增强类advisor
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(interceptor);
        return advisor;
    }

    @Bean
    public Advisor defaultPointcutAdvisor3() {
        MyAnnoInterceptor interceptor = new MyAnnoInterceptor();
        MyAnnotationAdvisor advisor = new MyAnnotationAdvisor(interceptor);
        advisor.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return advisor;
    }

    public static void main(String[] args) {
        Vector<Integer> vector = new Vector<>();
        vector.add(1);
        vector.remove(1);
        vector.get(1);
    }
}
